#ifndef __APP_COMMON_H__
#define __APP_COMMON_H__

#ifdef __cplusplus
extern "C"
{
#endif
#include <string.h>
#include "tkl_uart.h"
#include "tal_sw_timer.h"
#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"

/**************************************************************************
 *                              global  variable
 * ************************************************************************/
#define USER_KEY0           TUYA_GPIO_NUM_16
#define USER_PWM_CH0_PIN    TUYA_GPIO_NUM_18
#define USER_PWM_CH1_PIN    TUYA_GPIO_NUM_19
#define USER_UART0          TUYA_UART_NUM_0


#define KV_ID_RST_CNT 	    (0u)
#define KV_ID_WARM_ST 	    (1u)
#define KV_ID_APP_DATA 	    (2u)

#define ZCL_ID_ONOFF        100
#define ZCL_ID_MODE         101
#define ZCL_ID_BRIGHT       102
#define ZCL_ID_TEMPER       103
#define ZCL_ID_COUNT_DOWN   104
#define ZCL_ID_APP_DATA     105

#define PWM_DUTY_500        500
#define TWO_PWM_CHANNELS    2
#define PWM_FREQUENCY_1000  1000

#define C_NUM 5  // b5
#define W_NUM 10 // c2   pin num refer to soc_gpio.c

#define APP_WHITE_B_MAX     1000
#define APP_WHITE_T_MAX     1000
#define APP_COLOR_H_MAX     360
#define APP_COLOR_S_MAX     1000
#define APP_COLOR_V_MAX     1000

#define ZIGBEE_WHITE_B_MAX  255 ///< color: level
#define ZIGBEE_WHITE_T_MAX  255 ///< color: temperature
#define ZIGBEE_COLOR_H_MAX  254 ///< color: Hue
#define ZIGBEE_COLOR_S_MAX  254 ///< color: Saturation
#define ZIGBEE_COLOR_V_MAX  255 ///< color: value

#define ZIGBEE_JOIN_MAX_TIMEOUT         (30*1000)
#define ZIGBEE_HEAR_BEAT_DURATION       (150*1000) ///< 150s ~180s
#define NETWORK_JOIN_START_DELAY_TIME   1000        ///< ms
#define APP_CMD_HANDLE_DELAY_TIME       500

#define OPTION_ATTRIBUTE_ID             0x000F

#define BEACON_SEND_INTERVAL_200MS      200
#define ZIGBEE_JOIN_MAX_TIMEOUT_MS      (180*1000UL)

#define TY_CMD_SET_BRIGHT_CMD_ID                0xF0
#define TY_CMD_SET_COLOR_MODE_CMD_ID            0xF0    ///< change mode cmd: white/color/scene/music
#define TY_CMD_SET_COLOR_SCENE_CMD_ID           0xF1    ///< scene data cmd
#define TY_CMD_SET_COLOR_REALTIME_DATA_CMD_ID   0xF2    ///< real time ctrl cmd: mode(1) + H(2) + S(2) + V(2) + B(2) + T(2) = 11 bytes
#define TY_CMD_SET_COLOR_TEMPERATURE_CMD_ID     0xE0    ///<  cmd: temperature

///< color cluster
#define TY_ATTR_LIGHT_MODE_ATTRIBUTE_ID         0xF000  ///< light mode attribute (1 byte)
#define TY_ATTR_LIGHT_V_VALUE_ATTRIBUTE_ID      0xF001  ///< color V attribute (1 byte)
#define TY_ATTR_LIGHT_APP_DATA_ATTRIBUTE_ID     0xF102  ///< all app data attribute: mode(1) + H(1) + S(1) + V(1) + B(1) + T(2) = 7 Bytes
#define TY_ATTR_LIGHT_SCENE_DATA_ATTRIBUTE_ID   0xF003  ///< scene data attribute (2+8*n, n<=8) Bytes
#define TY_ATTR_CURRENT_BRIGHT_ATTR_ID          0xF000  ///<
#define TY_ATTR_COLOR_TEMPERATURE_ATTR_ID       0xE000  ///<

///< level cluster
#define TY_ATTR_CURRENT_BRIGHT_ATTR_ID 0xF000
///< on off cluster
#define TY_ATTR_COUNT_DOWN_ATTR_ID 0xF000


#ifndef OPRT_OK
#define OPRT_OK 0
#endif
#ifndef OPRT_INVALID_PARM
#define OPRT_INVALID_PARM 1
#endif
#ifndef OPRT_COM_ERROR
#define OPRT_COM_ERROR 2
#endif

#define PIN_NOEXIST 0xFF ///< default pin def

typedef struct
{
    UCHAR_T is_warm_start;
    UCHAR_T rst_cnt;
    UCHAR_T padding[2];
} RST_INFO_T;
typedef struct
{
    USHORT_T H;
    USHORT_T S;
    USHORT_T V;
} APP_HSV_T;
typedef struct
{
    UCHAR_T h;
    UCHAR_T s;
    UCHAR_T v;
} ZIGBEE_HSV_T;
typedef struct
{
    UCHAR_T onoff;
    UINT16_T bright;
    UINT16_T temperature;
} APP_DATA_T;
typedef struct
{
    UCHAR_T onoff;
    UCHAR_T mode;
    UINT16_T H;
    UINT16_T S;
    UINT16_T V;
    UINT16_T B;
    UINT16_T T;
} APP_ALL_DATA_T;

typedef enum
{
    ZIGBEE_CMD_SINGLE = 0,
    ZIGBEE_CMD_GROUP
} ZIGBEE_CMD_T;

/**************************************************************************
 *                              init functions
 * ************************************************************************/
/**
 * @berief: light init
 * @param [in] none 
 * @return: OPERATE_RET
 */
OPERATE_RET app_light_init(VOID_T);

/**************************************************************************
 *                          software timer functions
 * ************************************************************************/
/**
 * @brief software timer use id enum
 */
typedef enum
{
    CLEAR_RESET_CNT_SW_TIMER = 35, ///< basic timer id 35
    BLINK_SW_TIMER,
    COUNTDOWN_SW_TIMER,
    SHADE_TIMER,
    NETWORK_JOIN_START_DELAY_TIMER,
} SW_TIMER_ID_E;

  /**************************************************************************
 *                        pwm functions
 * ************************************************************************/

VOID_T app_button_init(VOID_T);
OPERATE_RET app_pwm_init(VOID_T);
VOID_T dev_uart_output(IN CONST CHAR_T *str);
VOID_T app_button_scan_cb(TIMER_ID timer_id, VOID_T *arg);

/**************************************************************************
 *                          flash functions
 * ************************************************************************/
extern UINT8_T g_power_on_state;
extern UINT16_T g_power_on_bright;
extern UINT16_T g_power_on_temperature;

UCHAR_T app_zcl_get_option_value(ZG_CLUSTER_ID_E cluster, UCHAR_T optionMask, UCHAR_T optionOverride);

#ifdef __cplusplus
}
#endif

#endif
